<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const clone = function clone(target, map = new Map) {
      if (typeof target === 'object') {
        let cloneValue = Object.prototype.toString.call(target) === '[object Array]' ? [] : {}
        if (map.get[target]) {
          return map.get[target]
        }
        for (let key in target) {
          cloneValue[key] = clone(target[key], map)
        }
        map.set(target, cloneValue)
        return cloneValue
      } else {
        return target
      }
    }
    const target = {
      field1: 1,
      field2: undefined,
      field3: {
        child: 'child'
      },
      field4: [2, 4, 8]
    }
    const a = clone(target)
    target.field3.child = '哈哈哈哈哈'
    console.log(a)
  </script>
</body>

</html>
